﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;

using OVS.Business;
using OVS.CoreDomain;
using OVS.CoreDomain.Business;

namespace OVS
{
    public partial class ElectionResults : System.Web.UI.Page
    {
        protected void Page_Load(object sender, EventArgs e)
        {
            if (!Page.IsPostBack)
            {
                IBusinessElectionResults businessElectionResults = new BusinessElectionResults();
                IElection election = new Election();
                election.ElectionId = 1;


                if (!businessElectionResults.ShowResults(election))
                {
                    divElectionResults.Visible = false;
                    divCannotShow.Visible = true;
                    IBusinessElection businessElection = new BusinessElection();
                    IElection _election = businessElection.GetElection(election.ElectionId);
                    lblCannotShow.Text = "The results will be available only on and after election result date! <br/> Please visit on or after:" + _election.ResultDate.ToShortDateString();
                }
                else
                {

                    divElectionResults.Visible = true;
                    divCannotShow.Visible = false;
                }
            }
                
        }

        protected void btnSearch_Click(object sender, EventArgs e)
        {
            IBusinessCandidate businessCandidate = new BusinessCandidate();

            IConstituency constituency = new Constituency();

            IDistrict district = new District();
            district.DistrictId = ((DropDownList)SelectWard.FindControl("ddlDistrict")).SelectedValue;

            IPanchayat panchayat = new Panchayat();
            panchayat.PanchayatId = ((DropDownList)SelectWard.FindControl("ddlPanchayat")).SelectedValue;

            IWard ward = new Ward();
            ward.WardId = ((DropDownList)SelectWard.FindControl("ddlWard")).SelectedValue;

            constituency.District = district;
            constituency.Panchayat = panchayat;
            constituency.Ward = ward;

            IElection election = new Election();

            constituency.Election = election;

            List<ICandidate> candidates = businessCandidate.GetCandidates(constituency);

            IBusinessElectionResults businessElectionResults = new BusinessElectionResults();
            IBusinessEncryptVotes businessEncryptVotes = new BusinessEncryptVotes();
            businessEncryptVotes.DecryptVotes(candidates);

            

            foreach (ICandidate candidate in candidates)
            {
                candidate.Voter.Photo = Page.ResolveClientUrl(@"~/Photos/Voters/" + candidate.Voter.VoterId + ".jpg");
                candidate.Symbol = Page.ResolveClientUrl(@"~/Photos/CandidateSymbols/" + candidate.Symbol + ".jpg");

            }

            gvCandidateList.DataSource = candidates;
            gvCandidateList.DataBind();

            ICandidate winner = businessElectionResults.GetWinner(candidates);
            imgParty.ImageUrl = Page.ResolveClientUrl(winner.Symbol);
            imgWinner.ImageUrl = Page.ResolveClientUrl(@"~/Photos/Voters/" + winner.Voter.VoterId + ".jpg");

            candidateName.Text = winner.Voter.Name;
            candidateParty.Text = winner.Party;
            candidateVotes.Text = winner.Votes.ToString();

            divResults.Visible = true;
        }
    }
}
